package com.lambda;

import java.util.ArrayList;

import java.util.List;

/**
 * 测试排序类
 *
 * @author liuyazhuang
 *
 */
public class SortTest {
    public static void sortTest() throws Exception {
        List<Employee> employeeList = new ArrayList<Employee>() {
            {
                add(new Employee(1, 9, 10000, 10));
                add(new Employee(2, 9, 12000, 7));
                add(new Employee(3, 5, 10000, 12));
                add(new Employee(4, 5, 10000, 6));
                add(new Employee(5, 3, 5000, 3));
                add(new Employee(6, 1, 2500, 1));
                add(new Employee(7, 5, 8000, 10));
                add(new Employee(8, 3, 8000, 2));
                add(new Employee(9, 1, 3000, 5));
                add(new Employee(10, 1, 2500, 4));
                add(new Employee(11, 2, 2000, 4));
            }
        };
        employeeList.sort((employee1,employee2)->{

            int cr = 0;
            //按级别降序排列
            int a = employee2.getLevel() - employee1.getLevel();
            if (a != 0) {
                cr = (a > 0) ? 1 : -1;
            } else {
                //按薪水降序排列
                a = employee2.getSalary() - employee1.getSalary();
                if (a != 0) {
                    cr = (a > 0) ? 1 : -1;
                } else {
                    //按入职年数降序排列
                    a = employee2.getYear() - employee1.getYear();
                    if (a != 0) {
                        cr = (a > 0) ? 1 : -1;
                    }
                }
            }
            return cr;
        });
        System.out.println("ID\tLevel\tSalary\tYears");
        System.out.println("=============================");
        for (Employee employee : employeeList) {
            System.out.printf("%d\t%d\t%d\t%d\n", employee.getId(), employee.getLevel(), employee.getSalary(),
                    employee.getYear());
        }
        System.out.println("=============================");
    }

    public static void main(String[] args) throws Exception {
        sortTest();
    }
}
